﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
    /// <summary>
    /// 数据访问类:tb_item
    /// </summary>
    public partial class tb_item
    {
        public tb_item()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tb_item");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
			};
            parameters[0].Value = id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Maticsoft.Model.tb_item model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_item(");
            strSql.Append("name,summary,description,stock,is_sale,img_orgin,img_thumb,price,expire_date,cost)");
            strSql.Append(" values (");
            strSql.Append("@name,@summary,@description,@stock,@is_sale,@img_orgin,@img_thumb,@price,@expire_date,@cost)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar,255),
					new MySqlParameter("@summary", MySqlDbType.VarChar,255),
					new MySqlParameter("@description", MySqlDbType.VarChar,999),
					new MySqlParameter("@stock", MySqlDbType.Int32,11),
					new MySqlParameter("@is_sale", MySqlDbType.Int32,1),
					new MySqlParameter("@img_orgin", MySqlDbType.VarChar,255),
					new MySqlParameter("@img_thumb", MySqlDbType.VarChar,255),
					new MySqlParameter("@price", MySqlDbType.Decimal,10),
					new MySqlParameter("@expire_date", MySqlDbType.DateTime),
					new MySqlParameter("@cost", MySqlDbType.Decimal,10)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.summary;
            parameters[2].Value = model.description;
            parameters[3].Value = model.stock;
            parameters[4].Value = model.is_sale;
            parameters[5].Value = model.img_orgin;
            parameters[6].Value = model.img_thumb;
            parameters[7].Value = model.price;
            parameters[8].Value = model.expire_date;
            parameters[9].Value = model.cost;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.tb_item model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_item set ");
            strSql.Append("name=@name,");
            strSql.Append("summary=@summary,");
            strSql.Append("description=@description,");
            strSql.Append("stock=@stock,");
            strSql.Append("is_sale=@is_sale,");
            strSql.Append("img_orgin=@img_orgin,");
            strSql.Append("img_thumb=@img_thumb,");
            strSql.Append("price=@price,");
            strSql.Append("expire_date=@expire_date,");
            strSql.Append("cost=@cost");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar,255),
					new MySqlParameter("@summary", MySqlDbType.VarChar,255),
					new MySqlParameter("@description", MySqlDbType.VarChar,999),
					new MySqlParameter("@stock", MySqlDbType.Int32,11),
					new MySqlParameter("@is_sale", MySqlDbType.Int32,1),
					new MySqlParameter("@img_orgin", MySqlDbType.VarChar,255),
					new MySqlParameter("@img_thumb", MySqlDbType.VarChar,255),
					new MySqlParameter("@price", MySqlDbType.Decimal,10),
					new MySqlParameter("@expire_date", MySqlDbType.DateTime),
					new MySqlParameter("@cost", MySqlDbType.Decimal,10),
					new MySqlParameter("@id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.summary;
            parameters[2].Value = model.description;
            parameters[3].Value = model.stock;
            parameters[4].Value = model.is_sale;
            parameters[5].Value = model.img_orgin;
            parameters[6].Value = model.img_thumb;
            parameters[7].Value = model.price;
            parameters[8].Value = model.expire_date;
            parameters[9].Value = model.cost;
            parameters[10].Value = model.id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_item ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
			};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_item ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.tb_item GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,name,summary,description,stock,is_sale,img_orgin,img_thumb,price,expire_date,cost from tb_item ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
			};
            parameters[0].Value = id;

            Maticsoft.Model.tb_item model = new Maticsoft.Model.tb_item();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.tb_item DataRowToModel(DataRow row)
        {
            Maticsoft.Model.tb_item model = new Maticsoft.Model.tb_item();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["name"] != null)
                {
                    model.name = row["name"].ToString();
                }
                if (row["summary"] != null)
                {
                    model.summary = row["summary"].ToString();
                }
                if (row["description"] != null)
                {
                    model.description = row["description"].ToString();
                }
                if (row["stock"] != null && row["stock"].ToString() != "")
                {
                    model.stock = int.Parse(row["stock"].ToString());
                }
                if (row["is_sale"] != null && row["is_sale"].ToString() != "")
                {
                    model.is_sale = int.Parse(row["is_sale"].ToString());
                }
                if (row["img_orgin"] != null)
                {
                    model.img_orgin = row["img_orgin"].ToString();
                }
                if (row["img_thumb"] != null)
                {
                    model.img_thumb = row["img_thumb"].ToString();
                }
                if (row["price"] != null && row["price"].ToString() != "")
                {
                    model.price = decimal.Parse(row["price"].ToString());
                }
                if (row["expire_date"] != null && row["expire_date"].ToString() != "")
                {
                    model.expire_date = DateTime.Parse(row["expire_date"].ToString());
                }
                if (row["cost"] != null && row["cost"].ToString() != "")
                {
                    model.cost = decimal.Parse(row["cost"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,name,summary,description,stock,is_sale,img_orgin,img_thumb,price,expire_date,cost ");
            strSql.Append(" FROM tb_item ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by price asc ");
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tb_item ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from tb_item T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tb_item";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

